﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.BLL
{
    public partial class AccessoriesQuotationService
    {

        #region 分页获取配件报价列表 + IQueryable<AccessoriesQuotation> GetPagingList(ServiceProviderModel model, out int total)
        /// <summary>
        /// 分页获取配件报价列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public List<AccessoriesQuotationViewModel> GetPagingList(AccessoriesQuotationModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<AccessoriesQuotation>();

            if (model.IsAvailable.HasValue)
                expressionWhere = expressionWhere.And(o => o.IsAvailable == model.IsAvailable.Value);
            if (model.ServiceProviderId > 0)
                expressionWhere = expressionWhere.And(o => o.ServiceProviderId == model.ServiceProviderId);
            //型号
            if (model.ProductModelId > 0)
                expressionWhere = expressionWhere.And(o => o.ProductModelId == model.ProductModelId);

            var expressionProductType = PredicateExtensions.True<ProductType>();
            var expressionProductBrands = PredicateExtensions.True<ProductBrands>();
            var expressionAccessoriesType = PredicateExtensions.True<AccessoriesType>();

            //设备类别:手机、洗衣机
            if (model.ProductTypeId > 0)
                expressionProductType = expressionProductType.And(o => o.ID == model.ProductTypeId);
            if (model.ProductBrandId > 0)
                expressionProductBrands = expressionProductBrands.And(o => o.ID == model.ProductBrandId);
            //配件等级
            if (model.AccessoriesLevelID > 0)
                expressionWhere = expressionWhere.And(o => o.AccessoriesLevelId == model.AccessoriesTypeId);
            //配件
            if (model.AccessoriesTypeId > 0)
                expressionAccessoriesType = expressionAccessoriesType.And(o => o.ID == model.AccessoriesTypeId);

            bool sort = true;
            if (!string.IsNullOrWhiteSpace(model.iSorts))
                sort = false;
            var result = (from a in LoadEntities(expressionWhere).AsNoTracking()
                          join b in ServiceFactory.ProductModelsServiceInstance.LoadEntities(t => true).AsNoTracking() on a.ProductModelId equals b.ID into temp1
                          from c in temp1.DefaultIfEmpty()
                          join d in ServiceFactory.ProductBrandsServiceInstance.LoadEntities(expressionProductBrands).AsNoTracking() on c.BrandId equals d.ID 
                          join f in ServiceFactory.AccessoriesLevelServiceInstance.LoadEntities(t => true).AsNoTracking() on a.AccessoriesLevelId equals f.ID into temp3
                          from g in temp3.DefaultIfEmpty()
                          join h in ServiceFactory.AccessoriesTypeServiceInstance.LoadEntities(expressionAccessoriesType).AsNoTracking() on g.AccessoriesTypeID equals h.ID
                          join j in ServiceFactory.ProductTypeServiceInstance.LoadEntities(expressionProductType).AsNoTracking() on h.ProductTypeID equals j.ID
                          join l in ServiceFactory.ServiceProviderServiceInstance.LoadEntities(t => true).AsNoTracking() on a.ServiceProviderId equals l.ID 
                          orderby sort ? a.ID:a.QuotationPrice descending
                          select new AccessoriesQuotationViewModel
                          {
                              ID = a.ID,
                              QuotationPrice = a.QuotationPrice,
                              QuotationTime=a.QuotationTime.Value,
                              LastModifyTime = a.LastModifyTime,
                              IsAvailable = a.IsAvailable,
                              ServiceType= a.ServiceType,
                              AccessoriesLevelName = g.AccessoriesLevelName,
                              ProductName = j.ProductName,
                              BrandName = d.BrandName,
                              ModelName = c.ModelName,
                              AccessoriesName = h.AccessoriesName,
                              ServiceProviderName = l.ServiceProviderName
                          }).Skip((model.PageIndex-1)* model.iDisplayLength).Take(model.iDisplayLength).ToList();

            total = (from a in LoadEntities(expressionWhere).AsNoTracking()
                     join b in ServiceFactory.ProductModelsServiceInstance.LoadEntities(t => true).AsNoTracking() on a.ProductModelId equals b.ID into temp1
                     from c in temp1.DefaultIfEmpty()
                     join d in ServiceFactory.ProductBrandsServiceInstance.LoadEntities(expressionProductBrands).AsNoTracking() on c.BrandId equals d.ID
                     join f in ServiceFactory.AccessoriesLevelServiceInstance.LoadEntities(t => true).AsNoTracking() on a.AccessoriesLevelId equals f.ID into temp3
                     from g in temp3.DefaultIfEmpty()
                     join h in ServiceFactory.AccessoriesTypeServiceInstance.LoadEntities(expressionAccessoriesType).AsNoTracking() on g.AccessoriesTypeID equals h.ID
                     join j in ServiceFactory.ProductTypeServiceInstance.LoadEntities(expressionProductType).AsNoTracking() on h.ProductTypeID equals j.ID
                     join l in ServiceFactory.ServiceProviderServiceInstance.LoadEntities(t => true).AsNoTracking() on a.ServiceProviderId equals l.ID
                     select new AccessoriesQuotationViewModel
                     {
                         ID = a.ID
                     }).Count();
            return result;
        }
        #endregion
    }
}
